package com.itstorm.pomeloweather.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.itstorm.pomeloweather.R;
import com.itstorm.pomeloweather.util.BroadcastCode;

public class SettingActivity extends Activity {

	private CheckBox cbBlur;
	private ImageButton imgbtnBack;
	private SharedPreferences weatherList;
	private SharedPreferences.Editor editor;
	private FrameLayout flBlur;
	private TextView tvBlur;
	private boolean blurred;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		weatherList = getSharedPreferences("WeatherList", 0);
		editor = weatherList.edit();
		blurred = weatherList.getBoolean("blurred", true);
		initialization();
	}

	private void initialization() {
		cbBlur = (CheckBox) findViewById(R.id.cb_blur);
		imgbtnBack = (ImageButton) findViewById(R.id.imgbtn_back);
		flBlur = (FrameLayout) findViewById(R.id.fl_blur);
		tvBlur = (TextView) findViewById(R.id.tv_blur);

		cbBlur.setChecked(blurred);
		if (blurred) {
			tvBlur.setText("已开启动态模糊");
		} else {
			tvBlur.setText("已关闭动态模糊");
		}
		flBlur.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				blurred = !blurred;
				if (blurred) {
					tvBlur.setText("已开启动态模糊");
				} else {
					tvBlur.setText("已关闭动态模糊");
				}
				cbBlur.setChecked(blurred);
				editor.putBoolean("blurred", blurred);
				editor.commit();
				Intent mIntent = new Intent();
				mIntent.setAction(BroadcastCode.changeBlur);
				sendBroadcast(mIntent);
			}
		});

		imgbtnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				overridePendingTransition(R.anim.fade, R.anim.fade);
				SettingActivity.this.finish();
			}
		});

	}
}
